package day0213;

import java.util.Scanner;

public class ArrayDemo02 {

    public static void main(String[] args) {
        /*
            在数组某个位置添加元素
         */

        int[] arr = {1, 2, 3, 4, 5};

        Scanner in = new Scanner(System.in);
        System.out.println("请输入添加元素的下标: ");
        int index = in.nextInt(); // 1
        System.out.println("请输入添加的数值： ");
        int num = in.nextInt(); // 9

        int[] arr1 = new int[arr.length + 1];

        arr1[index] = num;

        for (int i = 0, start = 0; i < arr1.length; i++) {
            if (i != index) {
                arr1[i] = arr[start ++];
            }
        }

        // 遍历
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i] + ", ");
        }
    }
}
